//index.js
//获取应用实例
const app = getApp()
const http = require('../../utils/http');
Page({
    data: {
        login: '../images/login.png',
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        mobile: '',
        isToken: false,
        iv: '',
        encryptedData: '',
        date: '请选择生日',
    },
    //事件处理函数

    onLoad: function () {

    },
    bindDateChange(e) {
        this.setData({
            date: e.detail.value
        })
    },
    bindGetUserInfo: function (e) {
        console.log(e);
        if (e.detail.userInfo) {
            console.log("用户按了允许授权按钮");
            app.globalData.userInfo = e.detail.userInfo
            // this.setData({
            //     userInfo: e.detail.userInfo
            // })

            if (this.data.date == "请选择生日") {
                wx.showToast({
                    title: '请选择生日',
                    icon: 'none',
                    duration: 2000
                });
            } else {
                const body = {
                    sessionKey: app._sessionKey,
                    encryptedData: e.detail.encryptedData,
                    ivStr: e.detail.iv,
                    openid: app._openid,
                    birthday: this.data.date
                }
                console.log(body);
                http._objPost("/mini/saveUserinfo", body).then(res => {
                    if (res.code == 200) {
                        wx.showToast({
                            title: '注册成功',
                            duration: 1500
                        });
                        setTimeout(function () {
                            wx.hideToast();
                            wx.navigateTo({
                                url: '../user/user'
                            })
                        }, 2000)

                    } else {
                        wx.showToast({
                            title: res.message,
                            icon: 'none',
                            duration: 2000
                        });
                        setTimeout(function () {
                            wx.hideToast()
                        }, 2000)
                    }
                }).catch(e => {
                    console.log(e)
                });
            }
        } else {
            console.log("用户按了拒绝按钮");
            wx.showToast({
                title: "为了您更好的体验,请先同意授权",
                icon: 'none',
                duration: 2000
            });
        }
    },
})
